<?php
//======================================================================================
//
// Function: Read integrationLog.sqlite
//
// Programmer: AR
// Date : 2025-05-27
//
// Copyright Reeft A/S (c) - 2025
//======================================================================================
// datatables parameters:
// https://datatables.net/manual/server-side
//======================================================================================
// General config
//======================================================================================
include "config/config.php";
//======================================================================================
// Set language
//======================================================================================
include "include/set_language.php";
//======================================================================================
// Get input
//======================================================================================
if (isset($_REQUEST["cust_number"])) {
$cust_number = $_REQUEST["cust_number"];
} else {
echo "ERROR: cust_number missing";
exit;
}
if (isset($_REQUEST["fromdate"])) {
$fromdate = $_REQUEST["fromdate"];
} else {
$fromdate = '';
}
if (isset($_REQUEST["fromtime"])) {
$fromtime = $_REQUEST["fromtime"];
} else {
$fromtime = '';
}
if (isset($_REQUEST["todate"])) {
$todate = $_REQUEST["todate"];
} else {
$todate = '';
}
if (isset($_REQUEST["totime"])) {
$totime = $_REQUEST["totime"];
} else {
$totime = '';
}
if (isset($_REQUEST["logType"])) {
$logType = $_REQUEST["logType"];
} else {
$logType = '';
}
if (isset($_REQUEST["logCode"])) {
$logCode = $_REQUEST["logCode"];
} else {
$logCode = '';
}
if (isset($_REQUEST["logPgm"])) {
$logPgm = $_REQUEST["logPgm"];
} else {
$logPgm = '';
}
if (isset($_REQUEST["clientID"])) {
$clientID = $_REQUEST["clientID"];
} else {
$clientID = '';
}
if (isset($_REQUEST["dataLine"])) {
$dataLine = $_REQUEST["dataLine"];
} else {
$dataLine = '';
}
// echo "$logType $logCode $logPgm";
// exit;
//===============================================================
// Pagination fields "local"/datatables
//===============================================================
if (isset($_REQUEST["rpyOffSet"])) {
$rpyOffSet = $_REQUEST["rpyOffSet"];
} else {
$rpyOffSet = 0;
}
if (isset($_REQUEST["rpyPageSize"])) {
$rpyPageSize = $_REQUEST["rpyPageSize"];
} else {
$rpyPageSize = 0;
}
if (isset($_REQUEST["rpyPageGoTo"])) {
$rpyPageGoTo = $_REQUEST["rpyPageGoTo"];
} else {
$rpyPageGoTo = 0;
}
//===============================================================
// Datatables input, will overrule "orgainal" fields if found
//===============================================================
if (isset($_REQUEST["start"])) {
$dataTablesOffset = $_REQUEST["start"];
} else {
$dataTablesOffset = 0;
}
if (isset($_REQUEST["length"])) {
$dataTablesPageLength = $_REQUEST["length"];
} else {
$dataTablesPageLength = 0;
}
if (isset($_REQUEST["search"]["value"])) {
$search_arg = $_REQUEST["search"]["value"];
} else {
$search_arg = '';
}
if (isset($_REQUEST['draw'])) {
$draw = $_REQUEST['draw'];
} else {
$draw = 1;
}
if (isset($_REQUEST["order"][0])) {
$rpyOrderColumn = '';
foreach ($_REQUEST["order"] as $key => &$tmpArr) {
// print_r($tmpArr);
$order_column_index = $tmpArr["column"];
$order_column_dir = $tmpArr["dir"];
if ($key == 0) $rpyOrderColumn = $_REQUEST["columns"][$order_column_index]["data"] . "|" . strtoupper($order_column_dir);
else $rpyOrderColumn .= "," . $_REQUEST["columns"][$order_column_index]["data"] . "|" . strtoupper($order_column_dir);
}
} else {
$rpyOrderColumn = '';
}
$dataLine = urlencode($dataLine);
$search_arg = str_replace(' ', "|", $search_arg);
// echo "rpyOrderColumn: $rpyOrderColumn";
// exit;
//======================================================================================
// Get input
//======================================================================================
if ( $dataTablesPageLength > 0 ) {
$rpyPageSize = $dataTablesPageLength;
}
if ( $dataTablesOffset > 0 ) {
$rpyOffSet = $dataTablesOffset;
}
//======================================================================================
// Get session variables
//======================================================================================
include "include/getsession.php";
//======================================================================================
// Set language
//======================================================================================
include "include/set_language.php";
//======================================================================================
// Connect to SQLite database in file
//======================================================================================
$entries_found = 0;
$returnCode = '99';
$returnMsg = 'No data found';
$currentDate = date('Y-m-d');
$currentTime = date('H:i:s');
//======================================================================================
// Set header
//======================================================================================
header('Content-Type: application/json;charset=utf-8');
//======================================================================================
// Get time stamp
//======================================================================================
$timeStamp = '';
if ( $timeStamp == '' ) {
$now = DateTime::createFromFormat('U.u', number_format(microtime(true), 6, '.', ''));
$timeStamp = $now->format("Y-m-d H:i:s");
}
$data = array(
"cust_number" => $cust_number,
"timestamp" => $timeStamp,
"rpyPageGoTo" => $rpyPageGoTo,
"rpyOffSet" => $rpyOffSet,
"rpyPageSize" => $rpyPageSize,
"rpyOrderColumn" => $rpyOrderColumn,
"fromdate" => $fromdate,
"fromtime" => $fromtime,
"todate" => $todate,
"totime" => $totime,
"logType" => $logType,
"logCode" => $logCode,
"logPgm" => $logPgm,
"clientID" => $clientID,
"dataLine" => $dataLine,
"draw" => $draw
);
//======================================================================================
// Make call to API somewhere
//======================================================================================
$jsonData = json_encode($data);
//header('Content-Type: application/json');
$json_pretty = json_encode(json_decode($jsonData), JSON_PRETTY_PRINT);
// echo $json_pretty;
// echo "<br>";
// echo $url;
$headers = array(
"Content-type: Content-Type:application/json",
"User-Agent: REEFTintegrationLog 1.0",
"APIKEY: $DFT_API_KEY"
);
$parms = '?'
. 'rpyPageGoTo=' . $rpyPageGoTo
. '&rpyOffSet=' . $rpyOffSet
. '&rpyPageSize=' . $rpyPageSize
. '&rpySearchArg=' . $search_arg
. '&rpyOrderColumn='. $rpyOrderColumn
. '&fromdate=' . $fromdate
. '&fromtime=' . $fromtime
. '&todate=' . $todate
. '&totime=' . $totime
. '&logType=' . $logType
. '&logCode=' . $logCode
. '&logPgm=' . $logPgm
. '&cust_number=' . $cust_number
. '&clientID=' . $clientID
. '&dataLine=' . $dataLine
. '&draw=' . $draw
;
// Set URL
$url = $DFT_WEBSERVICE_URL . '/' . 'customerlog_maintain_data.php';
// Add parms
$url = $url . $parms;
// echo $url;
// exit;
// Create a new cURL resource
$ch = curl_init($url);
// Attach encoded JSON string to the POST fields
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
// Set the content type to application/json
//curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers );
// Return response instead of outputting
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, false);
//curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, 'POST' );
curl_setopt($ch, CURLOPT_HEADER, false); // we do not need headers
curl_setopt($ch, CURLOPT_NOBODY, false); // we don't need body
// Execute the GET request
$result = curl_exec($ch);
$ary = curl_getinfo($ch);
// Close cURL resource
curl_close($ch);
$data = json_decode($result, true);
//print_r($result);
$returnResult = [];
$returnResult["header"] = $data['header'];
//$returnResult["header_buttons"] = $data['header_buttons'];
$returnResult["draw"] = intval( $draw );
$returnResult["recordsTotal"] = intval( $data['header']['recordsTotal'] );
$returnResult["recordsFiltered"] = intval( $data['header']['recordsFiltered'] );
$returnResult["data"] = $data['data'];
echo json_encode($returnResult);
?>